export function getToday() {
	let date = new Date();
	return date .getFullYear() + "-" + ((date .getMonth()+1) >= 10 ? (date .getMonth()+1) : '0'+(date .getMonth()+1)) + "-" +  date .getDate() ;
}
export function jsGetAge(strBirthday) {
  var returnAge
  var strBirthdayArr = strBirthday.split('-')
  var birthYear = strBirthdayArr[0]
  var birthMonth = strBirthdayArr[1]
  var birthDay = strBirthdayArr[2]

  let d = new Date()
  var nowYear = d.getFullYear()
  var nowMonth = d.getMonth() + 1
  var nowDay = d.getDate()

  if (nowYear == birthYear) {
    returnAge = 0 //同年 则为0岁
  } else {
    var ageDiff = nowYear - birthYear //年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        var dayDiff = nowDay - birthDay //日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      } else {
        var monthDiff = nowMonth - birthMonth //月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      }
    } else {
      returnAge = -1 //返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge //返回周岁年龄
}
export function getAgeByIdCard(identityCard) {
    var len = (identityCard + "").length;
    if (len == 0) {
        return '';
    } else {
        if ((len != 15) && (len != 18)) //身份证号码只能为15位或18位其它不合法
        {
            return '';
        }
    }
    var strBirthday = "";
    if (len == 18) //处理18位的身份证号码从号码中得到生日和性别代码
    {
        strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
    }
    if (len == 15) {
        strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
    }
    //时间字符串里，必须是“/”
    var birthDate = new Date(strBirthday);
    var nowDateTime = new Date();
    var age = nowDateTime.getFullYear() - birthDate.getFullYear();
    //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
    if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}
/**
* 获得相对当月AddMonthCount个月的起止日期
* AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
* ***/
export function getMonthStartAndEnd(AddMonthCount) {
	//起止日期数组
	var startStop = new Array();
	//获取当前时间
	var currentDate = new Date();
	var month=currentDate.getMonth()+AddMonthCount;
	if(month<0){
	  var n = parseInt((-month)/12);
	  month += n*12;
	  currentDate.setFullYear(currentDate.getFullYear()-n);
	}
	currentDate = new Date(currentDate.setMonth(month));
	//获得当前月份0-11
	var currentMonth = currentDate.getMonth();
	//获得当前年份4位年
	var currentYear = currentDate.getFullYear();
	//获得上一个月的第一天
	var currentMonthFirstDay = new Date(currentYear, currentMonth,1);
	//获得上一月的最后一天
	var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0);
	//添加至数组
	startStop.push(getDateStr3(currentMonthFirstDay));
	startStop.push(getDateStr3(currentMonthLastDay));
	//返回
	return startStop;
}
//获取当前日期yy-mm-dd
//date 为时间对象
function getDateStr3(date) {
	var year = "";
	var month = "";
	var day = "";
	var now = date;
	year = ""+now.getFullYear();
	if((now.getMonth()+1)<10){
	  month = "0"+(now.getMonth()+1);
	}else{
	  month = ""+(now.getMonth()+1);
	}
	if((now.getDate())<10){
	  day = "0"+(now.getDate());
	}else{
	  day = ""+(now.getDate());
	}
	return year+"-"+month+"-"+day;
}

/**
 * 空判断
 * @param str
 * @returns {boolean}
 */
module.exports.isNull=function (str){
  if(str == null || str.length === 0 || str === ''){
    return true;
  }else{
    return false
  }
}
/**
 * 格式化请求参数
 */
module.exports.getParams=function (form) {
  let params=new URLSearchParams(); //定义空的查询字符串
  let keys2 = Object.keys(form);//获取表单对象的keys
  keys2.map(function (name) {
    params.append(name,form[name]);//将key、value封装到查询字符串
  });
  return params;
}
/**
 *
  * @param str
 * @param regStr
 * @returns {boolean}
 */
module.exports.strIndexOf=function(str,regStr){
  if(str.indexOf(regStr)>=0){
    return true;
  }else{
    return false;
  }
}
/**
 * 时间格式化
 * @param number
 * @param format
 * @returns {*}
 */
module.exports.formatTime=function(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];
  var date = new Date(number);

  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 取万四舍五入
 */
module.exports.amountSf = function (value){
  var resValue = value*1/10000;
  return resValue.toFixed(2);
}
/**
 * 四舍五入
 */
module.exports.valueFixed = function (value){
  var resValue = value*1;
  return resValue.toFixed(0);
}

module.exports.valueCeil=function(value){
  return Math.ceil(value*1/100)/100
}
module.exports.mobileFormat=function(value){
  if (value){
    return value.substring(0, 3) + '****' + value.substring(value.length - 4);
  }else{
    return value;
  }
}
module.exports.formatBank=function(value){
	if (value){
	  return value.substring(0,4)+"********"+value.substring(value.length-4,value.length);
	}else{
	  return value;
	}
}
module.exports.formatBankLastFour=function(value){
	if (value){
	  return value.substring(value.length-4,value.length);
	}else{
	  return value;
	}
}
module.exports.formatIdNum=function(value){
	if (value){
	  return value.substring(0,6)+"********"+value.substring(value.length-4,value.length);
	}else{
	  return value;
	}
}
module.exports.formatName=function(value){
	if(value=='' || null==value){
    return "--"
  }else{
    return value.substring(0,1)+'**';
  }
}
// 返回值为空的时候
module.exports.formatKong=function(value){
	if(value=='' || null==value || value.length === 0){
    return "--"
  }else{
	  return value;
	}
}
// 获取银行卡后四位
module.exports.bankLast=function(value){
	if(value=='' || null==value || value.length === 0){
    return "--"
  }else{
	  var str = value;
	  return str.substring(str.length-4);
	}
}

// ****
module.exports.numToStr=function(value1,value2){
 return value1+'-'+ value2;
}
module.exports.getUrlParam=function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		var r = window.location.search.substr(1).match(reg);  //匹配目标参数
		if (r != null) return decodeURI(r[2]); return null; //返回参数值
}
/**
 * 获取页面参数
 */
module.exports.getQueryString =  function(name) {
	const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	const search =  window.location.search.split('?')[1] || '';
	const r = search.match(reg) || [];
	return r[2];
}
module.exports.getCookie=function(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg)) return decodeURI(arr[2]);
	else return null;
}

/**
 * @param {Object} isIos
 * 打开GPS定位服务
 */
let isIos
// #ifdef APP-PLUS
isIos = (plus.os.name == "iOS")
// #endif

function openAllGPS(isIos) {
	let content = isIos ? "为保证签到成功，请打开系统设置->隐私->定位服务->开启定位服务" : "为保证签到成功，请务必开启GPS定位服务！"
	uni.showModal({
		title: '警告',
		content: content,
		showCancel: false, // 不显示取消按钮
		success(res) {
			if (res.cancel) {
				return plus.runtime.quit();
			}
			if (isIos) {
				var UIApplication = plus.ios.import("UIApplication");
				var application2 = UIApplication.sharedApplication();
				var NSURL2 = plus.ios.import("NSURL");
				// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");		
				var setting2 = NSURL2.URLWithString("app-settings:");
				application2.openURL(setting2);

				plus.ios.deleteObject(setting2);
				plus.ios.deleteObject(NSURL2);
				plus.ios.deleteObject(application2);
			} else {
				var Intent = plus.android.importClass('android.content.Intent');
				var Settings = plus.android.importClass('android.provider.Settings');
				var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				var main = plus.android.runtimeMainActivity();
				main.startActivity(intent); // 打开安卓系统设置GPS服务页面
			}
		}
	});
}
/**
 * @param {Object} lat1
 * @param {Object} lng1
 * @param {Object} lat2
 * @param {Object} lng2
 * 计算坐标得距离
 */
function GetDistance(lat1, lng1, lat2, lng2) {
	var radLat1 = Rad(lat1);
	var radLat2 = Rad(lat2);
	var a = radLat1 - radLat2;
	var b = Rad(lng1) - Rad(lng2);
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; //输出为公里
	//s=s.toFixed(4);
	return s * 1000;
}
function Rad(d) {
	return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
}
module.exports.parseTime = function(time, format = 'yyyy-MM-dd hh:mm:ss') {
  if (arguments.length === 0) {
    return null
  }

  if (!time) {
    return time
  }

  let date

  if (typeof time === 'object') {
    date = time
  } else if (typeof time === 'string') {
    date = new Date(time)
  } else if (typeof time === 'number') {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }

  const o = {
    'M+': date.getMonth() + 1, // month
    'd+': date.getDate(), // day
    'h+': date.getHours(), // hour
    'm+': date.getMinutes(), // minute
    's+': date.getSeconds(), // second
    'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
    'S': date.getMilliseconds()
    // millisecond
  }

  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }

  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return format
}